package org.noote.spidy.monitor;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;

import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class Settings_Activity extends Activity {
	
	private static final int GETTING_DEVICE = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		
		ListView listSettings = (ListView)findViewById(R.id.listSettings);
		listSettings.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				goSettingsPage(position);
			}
		});
		
		View viewSelectedDevice = findViewById(R.id.viewSelectedDevice);
		viewSelectedDevice.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v) {
				// TODO !!!
			}
		});
		
		loadSettings();
		
		createList();
		goSettingsPage(0);
	}
	
	private void loadSettings() {
		SharedPreferences settings = getSharedPreferences(Spidy_Activity.PREFS_NAME, 0);
		
		// WiFi
        String sAddressDevice = settings.getString(Spidy_Activity.PREFS_NAME_IPADDRESS, "(Click here to select a device)");
		TextView textIPSelectedDevice = (TextView)findViewById(R.id.textIPSelectedDevice);
		textIPSelectedDevice.setText(sAddressDevice);
		
        try {
			String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
	        int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
	        
	        TextView textVersion = (TextView)findViewById(R.id.textVersion);
	        textVersion.setText(Html.fromHtml("<i>version "+versionName+" build "+Integer.toString(versionCode)+"</i>"));
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}

	protected void goSettingsPage(int iPage) {
		ViewFlipper _flipper = (ViewFlipper)findViewById(R.id.flipperSettings);
		_flipper.setInAnimation(this, R.anim.view_transition_in);
		_flipper.setOutAnimation(this, R.anim.view_transition_out);
		_flipper.setDisplayedChild(iPage);
	}
	
	void createList()
	{
		ListView listSettings = (ListView)findViewById(R.id.listSettings);
		
		ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;
        
        map = new HashMap<String, String>();
        map.put("title", getString(R.string.text_title_version));
        map.put("subtitle", getString(R.string.text_subtitle_version));
        listItem.add(map);
        
        map = new HashMap<String, String>();
        map.put("title", getString(R.string.text_title_wifi));
        map.put("subtitle", getString(R.string.text_subtitle_wifi));
        listItem.add(map);
        
		SimpleAdapter adapter = new SimpleAdapter (this.getBaseContext(), listItem, android.R.layout.simple_list_item_2,
    			new String[] {"title", "subtitle"}, new int[] {android.R.id.text1, android.R.id.text2});
		
		listSettings.setAdapter(adapter);
	}
	
	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data)
	{
		switch(requestCode)
		{
		case GETTING_DEVICE:
			if(resultCode == RESULT_OK)
			{
			}
			break;
		}
	}
}
